﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TransCode.Entitys;

namespace TransCode.Player.VideoPage
{
    /// <summary>
    /// Interaction logic for TargetItemControl.xaml
    /// </summary>
    public partial class TargetItemControl : UserControl
    {
        public TargetItemControl()
        {
            InitializeComponent();
        }

        public static DependencyProperty TargetProperty;
        public static DependencyProperty VideoProperty;

        static TargetItemControl()
        {
            TargetProperty = DependencyProperty.Register("Target",
                typeof(Target), typeof(TargetItemControl),
                new PropertyMetadata(null));

            VideoProperty = DependencyProperty.Register("Video",
                typeof(Video), typeof(TargetItemControl),
                new PropertyMetadata(null));
        }


        public Target Target
        {
            get { return this.GetValue(TargetItemControl.TargetProperty) as Target; }
            set { this.SetValue(TargetItemControl.TargetProperty, value); }
        }

        public Video Video
        {
            get { return this.GetValue(TargetItemControl.VideoProperty) as Video; }
            set { this.SetValue(TargetItemControl.VideoProperty, value); }
        }

        public IVideoPlayer VideoPlayer { get; set; }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (Video != null)
            {
                BitmapImage source = new BitmapImage();
                source.BeginInit();
                source.UriSource = new Uri(Target.ImagePath + Target.ImageName, UriKind.RelativeOrAbsolute);
                source.EndInit();
                img.Source = source;
                var width = canvas.ActualWidth;

                border.Width = (Target.EndTime - Target.BeginTime).TotalSeconds / Video.NaturalDuration.TotalSeconds * width;
                var left = Target.BeginTime.TotalSeconds / Video.NaturalDuration.TotalSeconds * width;
                 
                border.Margin = new Thickness(left, 0, 0, 0);
            }
        }

        private void img_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            VideoPlayer.Position = Target.BeginTime;
        }


    }
}
